package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * Created by fenglin on 2020/9/16 3:35 PM
 */
@Anoymous
@RestController
@RequestMapping("/shopping")
public class ShoppingController {
    @Reference(check = false,timeout = 2000,retries = 0)
    IProductCateService productCateService;

    @GetMapping("categories")
    public ResponseData getCategories(){
        AllProductCateRequest request = new AllProductCateRequest();

        AllProductCateResponse response = productCateService.getAllProductCate(request);

        if(response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){

            ResponseData responseData = new ResponseUtil().setData(response.getProductCateDtoList());

            return responseData;

        }else {

            return null;
        }

    }

    @Reference(check = false,timeout = 2000,retries = 0)
    IProductService productService;
    //以下三个都使用productService

    @GetMapping("/product/{id}")
    public ResponseData getProduct(@PathVariable("id") Long id){
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = productService.getProductDetail(request);

        if(response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){

            ResponseData responseData = new ResponseUtil().setData(response.getProductDetailDto());

            return responseData;

        }else {

            return null;
        }

    }

    //page=1&size=20&sort=1&priceGt=10&priceLte=500
    @GetMapping("/goods")
    public ResponseData getGoods(@RequestParam("page") Integer page,
                                 @RequestParam("size") Integer size,
                                 @RequestParam("sort") String sort,
                                 @RequestParam("priceGt") Integer priceGt,
                                 @RequestParam("priceLte") Integer priceLte){
        AllProductRequest request = new AllProductRequest();
        request.setPage(page);
        request.setSize(size);
        request.setSort(sort);
        request.setPriceGt(priceGt);
        request.setPriceLte(priceLte);

        AllProductResponse response = productService.getAllProduct(request);

        if(response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            ResponseData responseData = new ResponseUtil().setData(response.getProductDtoList());
            return responseData;
        }else {
            return null;
        }

    }

    @GetMapping("/recommend")
    public ResponseData getRecommend(){
        RecommendResponse response = productService.getRecommendGoods();
        if(response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            ResponseData responseData = new ResponseUtil().setData(response.getPanelContentItemDtos());
            return responseData;
        }else {
            return null;
        }
    }

}
